package com.zwj.controller.ordermaster;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mai.cart.Cart;
import com.mai.cart.CartItem;
import com.zwj.entity.ZOrderMaster;
import com.zwj.service.ZOrderMasterService;

@Controller
public class ZOrderMasterController {

	@Autowired
	private ZOrderMasterService zOrderMasterService;
	
	//查询所有订单
	@RequestMapping("/selectAll")
	public String selectAll(Model model,ZOrderMaster or,@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<ZOrderMaster> lom = this.zOrderMasterService.selectAll();
		PageInfo<ZOrderMaster> page=new PageInfo<ZOrderMaster>(lom);
		model.addAttribute("lom", lom);
		model.addAttribute("page", page);
		return "houtai";
	}

	
	//删除订单
	@ResponseBody
	@RequestMapping("/deleteOrderMaster")
	public String deleteOrderMaster(@Param("buyer_name")String buyer_name) {
		boolean lom = this.zOrderMasterService.deleteOrderMaster(buyer_name);
		if(lom) {
			System.out.println("删除成功");
			return "success";
		}else {
			System.out.println("删除失败");
			return "fail";
		}
		
	}
	

	
	//多个模糊查询
	@RequestMapping("/selectByLikeOrderMaster")
	public String selectByLikeOrderMaster(ZOrderMaster orderMaster,@Param("order_id")String order_id,@Param("buyer_name")String buyer_name,
			@Param("buyer_address")String buyer_address,Model model,HttpServletRequest request) {
//		orderMaster.setOrder_id(1);
		orderMaster.getOrder_id();
		request.getSession().getAttribute(buyer_name);
		orderMaster.setBuyer_name(buyer_name);
		request.getSession().setAttribute("order_id",orderMaster);
		request.getSession().getAttribute(order_id);
		List<ZOrderMaster> lom = this.zOrderMasterService.selectByLikeOrderMaster(orderMaster);
		model.addAttribute("lom", lom);
		return "/houtai.jsp";
	}
	
	@RequestMapping("/selectByLikebuyer_name")
	public String selectByLikebuyer_name(String buyer_name,Model model) {
		List<ZOrderMaster> lom = this.zOrderMasterService.selectByLikebuyer_name(buyer_name);
		model.addAttribute("lom", lom);
		return "/houtai.jsp";
	}
	
	//依据buyer_name查询
	@RequestMapping("/selectBybuyer_name")
	public String selectBybuyer_name(@Param("buyer_name")String buyer_name,Model model) {
		ZOrderMaster lom = this.zOrderMasterService.selectBybuyer_name(buyer_name);
		model.addAttribute("lom", lom);
		if(lom!=null) {
			System.out.println("查询成功");
		}else {
			System.out.println("查询失败");
		}
		return "ShoppingCart";
	}
	
	//修改收货地址
	@RequestMapping("/updateAddress")
	public String updateAddress(ZOrderMaster orderMaster,Model model) {
		boolean result = this.zOrderMasterService.updateAddress(orderMaster);
		if(result) {
			System.out.println("修改成功");
		}else {
			System.out.println("修改失败");
		}
		return "selectBybuyer_name";
	}
	
	@ResponseBody
	@RequestMapping("/addOrderMaster")
	public String addOrderMaster(String buyer_name, String buyer_phone, String buyer_address, double order_amount, int user_id, HttpServletRequest request) {
		ZOrderMaster z = new ZOrderMaster(buyer_name,buyer_phone,buyer_address,order_amount,user_id);
		int result = this.zOrderMasterService.addOrderMaster(z);
		request.getSession().setAttribute("order_id",z.getOrder_id());
		request.getSession().setAttribute("zongjia",order_amount);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("/upStatus")
	public String upStatus(int order_id, HttpServletRequest request) {
		if(this.zOrderMasterService.upStatus(order_id)) {
			return "success";
		}else {
			return "fail";
		}	
	}
	
}
